﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wys.DataForm.CSharp
{
    public partial class TypeUpdate : Wys.Control.Vb.UpdateFormTreeBase
    {
        public TypeUpdate()
        {
            InitializeComponent();
        }

        public override void InitUpdate()
        {
            base.InitUpdate();
            this.TextBrowser = 选项参数_窗口标题;
            this.Text = 选项参数_窗口标题;
        }

        #region 窗体参数

        private string _选项参数_窗口标题 = "";
        public string 选项参数_窗口标题
        {
            get
            {
                return _选项参数_窗口标题;
            }
            set
            {
                _选项参数_窗口标题 = value;
                TextBrowser = _选项参数_窗口标题;
            }
        }
        /// <summary>
        /// 1 BUG 2 语言类型 3 项目类型 4 网址类型 5 英语文章
        /// </summary>
        private string _选项参数_窗体类型 = "";
        public string 选项参数_窗体类型
        {
            get
            {
                return _选项参数_窗体类型;
            }
            set
            {
                _选项参数_窗体类型 = value;
                TypeId = Convert.ToInt32(选项参数_窗体类型);
            }
        }
        #endregion

        /// <summary>
        /// 1 BUG 2 语言类型 3 项目类型 4 网址类型 5 英语文章  9 为记账入 10 记账出 15 软件
        /// </summary>
        private string _选项参数_表名 = "XSCCODE";
        public string 选项参数_表名
        {
            get
            {
                return _选项参数_表名;
            }
            set
            {
                _选项参数_表名 = value;
                if (value == "")
                {
                    _选项参数_表名 = "XSCCODE";
                }               
            }
        }      

        public override bool DoLinkTable()
        {
            StringBuilder sb = new StringBuilder();
            string[] result = GetChildIdAndSelf(UTIL.CUtil.ConvertText(strID));
            String strAppend = "select * from " + 选项参数_表名 + " where ";
            for (int i = 0; i < result.Length; i++)
            {
                if (i == 0)
                {
                    strAppend = strAppend + " type in ( " + result[i] + ",";
                }
                if (i == result.Length - 1)
                {
                    strAppend = strAppend + result[i] + " ) ";
                }
                else
                {
                    strAppend = strAppend + result[i] + ",";
                }
            }

            if (ct.GetExistSql(strAppend))
            {
                ShowMessageBox("其他表关联此数据，不能删除");
                return false;
            }
            else
            {
                return true;
            }
        }

        private String[] GetChildIdAndSelf(String strParentId)
        {
            List<String> result = new List<string>();
            result.Add(strParentId);
            ADODB.Recordset rs = null;
            if (!String.IsNullOrEmpty(strParentId))
            {
                rs = ct.GetTable("select ID from XscBugType where ParentID='" + strParentId + "'");
            }
            else
            {
                rs = ct.GetTable("select ID from XscBugType where ParentID is null");
            }
            if (rs == null)
            {
                return null;
            }
            if (rs.EOF)
            {
                goto end1;
            }
            String str = "";
            String strid1 = "";
            while (!rs.EOF)
            {
                str = rs.Fields["ID"].Value.ToString();
                strid1 = rs.Fields["ID"].Value.ToString();
                if (!result.Contains(str))
                {
                    result.Add(str);
                }
                String[] resultChid = GetChildIdAndSelf(strid1);
                if (null == resultChid)
                {
                    goto end2;
                }
                foreach (String strItem in resultChid)
                {
                    if (!result.Contains(strItem))
                    {
                        result.Add(strItem);
                    }
                }
            end2:
                rs.MoveNext();
            }
        end1:
            rs.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
            rs = null;
            return result.ToArray();

        }
        public override bool DoInsert()
        {
            TypeAdd ta = new TypeAdd();
            ta.strName = this.strName;
            ta.TypeId = this.TypeId;
            ta.iType = this.iType;
            ta.选项参数_窗口标题 = this.选项参数_窗口标题;
            ta.选项参数_窗体类型 = this.选项参数_窗体类型;
            ta.ShowDialog();
            if (ta.bAdd)
            {
                RefreshData();
            }
            return true;
        }

        public override void 更新事件(TreeNode ND)
        {
            TypeAdd ta = new TypeAdd();
            ta.模式 = Wys.Control.Vb.InsertFormBase.模式型.修正;
            ta.参数列表.Add("ID", this.strID);
            ta.strName = this.strName;
            ta.TypeId = this.TypeId;
            ta.iType = this.iType;
            ta.选项参数_窗口标题 = this.选项参数_窗口标题;
            ta.选项参数_窗体类型 = this.选项参数_窗体类型;
            ta.ShowDialog();
            if (ta.bAdd)
            {
                RefreshData();
                //if (ND != null)
                //{
                //    this.MyTreeView1.SelectedNode = ND;
                //}
            }
        }
    }

   
}
